<?php
require '../classes/includeAll.php';
$titel="Upload - Infrastructure";
$head='<link rel="stylesheet" type="text/css" href="../design/css/upload.css">'
. "<link rel=\"stylesheet\" type=\"text/css\" href=\"../design/css/patchpanel.css\">
<script type=\"text/javascript\" src=\"../javascript/pagesJavascript/patchpanel.js\"></script>	
    ";
require '../menu/head.php';
require_once '../resources/phpFunctions.php';
require '../resources/getConnection.php';




//down below is the code for uploading a .csv file
$sent=false;
echo"<div id=\"upContainer\">";
if(isset($_POST['submit'])){                 //Routine wenn hinzufügen geklickt wurde
    $sent=true;
    
    if($_FILES['datei'] == ''){                //überprüft die Datei
    }  
    else{   //Überprüft ob das richtige Format vorhanden ist 
        $allowed_types = "csv";	// Erlaubte Dateitypen
        if(preg_match("/\." . $allowed_types . "$/i", $_FILES["datei"]["name"])){
           $data = $_FILES['datei']['name'];
        }
        else{ 
        }
    }
    
    $table =  fopen($_FILES["datei"]["tmp_name"],'r');
    
    $log=array();
    $firstLine = TRUE;
    
    $entry = new roomAndLevel($connection);
    
    while($csvLine=fgetcsv($table,1024,';')){
        if($firstLine){
            $firstLine = FALSE;
        }
        else {
            $entry->SwitchPortName = utf8_encode($csvLine[0]);
            $entry->SwitchName = utf8_encode($csvLine[1]);
            $entry->PatchPanelName = utf8_encode($csvLine[2]);
            $entry->RoomName = utf8_encode($csvLine[3]);
            $entry->RoomNumber = utf8_encode($csvLine[4]);
            $entry->LevelName = utf8_encode($csvLine[5]);
            
            $entry->AllExistsOrCreate();
            array_push($log, "Eintrag für Switchport $entry->SwitchPortName erstellt.");
        }
    }
}
if(!$sent){    
    echo "<h1>Upload:</h1>"
    . '<div id="contentLeft">'
    . "<div id=\"textbox\">"
    . "Diese Seite ermöglicht es Ihnen eine Patchpanelliste in die Datenbank zu laden. Hierzu steht eine Vorlage zum Download bereit. Die Vorlage kann mit einem beliebigen Tabellenkalkulationsprogramm oder einem Texteditor bearbeitet werden. Die erste Zeile muss erhalten bleiben.</br>"
    . "<input type=\"button\" value=\"Download\" onclick=\"window.open('../resources/vorlage_infrastructure.csv')\"></input>"
    . "</div></div>"
    . "<div id=\"contentRight\">"
    . "<form action=\"../pages/infrastructure.php\" method=\"post\" enctype=\"multipart/form-data\">"
    . "<span>W&auml;hlen Sie eine CSV-Datei von Ihrem Rechner aus (max. 2MB):<br>"
    . "<input name=\"datei\" type=\"file\" size=\"50\" maxlength=\"262144\" accept=\".csv\">"
    . "<input type=\"submit\" value=\"submit\" name=\"submit\" /></span>"
    . "</form>"
    . "</div></div><div id=\"sepperator\">"
    . "</div><div id=\"table\">";
}



$roomAndLevel = new roomAndLevel($connection);
$patchpanelIDs =$roomAndLevel->getAllPatchpanelIDs();

    echo"
        <h1>Geb&auml;udestruktur:</h1>
<div id=\"filter\">
    <div id=\"filterh2\">
        <h2>Filter:</h2>
    </div>
    
    <div id=\"radioButtons\">
        <input type=\"radio\" name=\selectSearch\" class=\"selectSearch\" value=\"name\" id=\"selectSwitchport\" checked=\"checked\"/> Switchport:<br>
        <input type=\"radio\" name=\selectSearch\" class=\"selectSearch\" value=\"invNumber\"  id=\"selectSwitch\"/> Switch:
    </div>
    <div id=\"radioButtons\">
        <input type=\"radio\" name=\selectSearch\" class=\"selectSearch\" value=\"name\" id=\"selectPatchpanel\" /> Patchpanel:<br>
        <input type=\"radio\" name=\selectSearch\" class=\"selectSearch\" value=\"invNumber\"  id=\"selectRoom\"/> Raum:
    </div>
        <div id=\"radioButtons\">
        <input type=\"radio\" name=\selectSearch\" class=\"selectSearch\" value=\"name\" id=\"selectRoomNr\" /> Raum Nr.<br>
        <input type=\"radio\" name=\selectSearch\" class=\"selectSearch\" value=\"invNumber\"  id=\"selectLevel\"/> Stockwerk:
    </div>
    <div id=\"searchField\">
        <input type=\"text\" name=\"searchInput\" value=\"\" id=\"searchInput\" />
    </div>
    <div id=\"load\"></div>
     <div id=\"button\"><input type=\"button\" value=\"Ausgew&auml;hlte l&ouml;schen\" name=\"search\" id=\"delete\" /></div>
</div>

<form id=\"update\">
<table id=\"patchList\" >
    <thead>
        <tr>
            <th><input type=\"checkbox\" name=\"checkAllEntries\" value=all\" id=\"checkAllEntries\" /></th>
            <th>Switchport:</th>
            <th>Switch:</th>
            <th>Patchpanel:</th>
            <th>Raum:</th>
            <th>Raum Nr.:</th>
            <th>Stockwerk:</th>
            <th></th>
        </tr>
    </thead>";    
foreach ($patchpanelIDs as $ID) 
{
    $roomAndLevel->ID = $ID;
    $roomAndLevel->getInfo();
echo"<tr panelID=\"$ID\">
        <td><input type=\"checkbox\" name=\"del[$ID]\" value=\"\" class=\"deleteEntries\" /></td>
        <td name=\"entry[$ID][0]\" info=\"switchPort\">".  
            sanitizeOutputString($roomAndLevel->SwitchPortName)
        ."</td><td name=\"entry[$ID][1]\" info=\"switch\">".
            sanitizeOutputString($roomAndLevel->SwitchName)
        ."</td><td name=\"entry[$ID][2]\" info=\"patchName\">".         
            sanitizeOutputString($roomAndLevel->PatchPanelName)
        ."</td><td name=\"entry[$ID][3]\" info=\"roomName\">".
            sanitizeOutputString($roomAndLevel->RoomName)
        ."</td><td name=\"entry[$ID][4]\" info=\"roomNumber\">".
            sanitizeOutputString($roomAndLevel->RoomNumber)
        ."</td><td name=\"entry[$ID][5]\" info=\"levelName\">".
            sanitizeOutputString($roomAndLevel->LevelName)
        ."</td>       
        <td patchpanelID=\"$ID\"><img src=\"../design/pictures/delete.png\" width=\"20\" alt=\"X\"/></td>";
}
echo"</table>
<p id=\"submitArea\">
</form>
<form id=\"new\">
    <table id=\"newFormTable\">
      <tbody>
        <tr>
          <td><input list=\"switchPort\" type=\"text\" name=\"switchPortNew\" value=\"\" class=\"inputBoxes\" placeholder=\"Switchport\" /></td>
          <td><input list=\"switch\" type=\"text\" name=\"switchNew\" value=\"\" class=\"inputBoxes\" placeholder=\"Switch Name\" /></td>
          <td><input list=\"patchName\" type=\"text\" name=\"patchNameNew\" value=\"\" class=\"inputBoxes\" placeholder=\"Patchpanel Name\" /></td>
          <td><input list=\"roomName\" type=\"text\" name=\"roomNameNew\" value=\"\" class=\"inputBoxes\" placeholder=\"Raum Name\" /></td>
          <td><input list=\"roomNumber\" type=\"text\" name=\"roomNumberNew\" value=\"\" class=\"inputBoxes\" placeholder=\"Raum Nummer\" /></td>
          <td><input list=\"levelName\" type=\"text\" name=\"levleNameNew\" value=\"\" class=\"inputBoxes\" placeholder=\"Stockwerk Name\" /></td>
        </tr>
        <tr>
          <td><input type=\"button\" value=\"Hinzuf&uuml;gen\" name=\"newEntry\" id=\"newEntry\" /></td>
          <td></td>
          <td></td>
          <td></td>
          <td></td>
          <td></td>
        </tr>
      </tbody>
    </table>
</form>


<datalist id=\"switchPort\">
<select>
";
$allSwitchPorts = $roomAndLevel->GetAllSwitchPorts();
foreach ($allSwitchPorts as $switchPort) 
    {
        echo"<option value=\"". $switchPort['name']."\"/>";
    }
echo"
</select>    
</datalist>
<datalist id=\"switch\">
";
$allSwitch= $roomAndLevel->GetAllSwitch();
foreach ($allSwitch as $switch) 
    {
        echo"<option value=\"". $switch['name']."\"/>";
    }
echo"
</datalist>
<datalist id=\"roomName\">
";
$allRoom= $roomAndLevel->GetAllRoom();
foreach ($allRoom as $room) 
    {
        echo"<option value=\"". $room['name']."\"/>";
    }
echo"
</datalist>
<datalist id=\"levelName\">
";
$allLevel= $roomAndLevel->getAllLevel();
foreach ($allLevel as $level) 
    {
        echo"<option value=\"". $level['name']."\"/>";
    }
echo"
</datalist>
</div>
";


//Disconect DB & Footer
 $connection->disconnectDB();
require '../menu/footer.php';